package xj.graph2d.template.uml;

import xj.graph2d.AbstractModelListener;
import xj.graph2d.DrawShape;
import xj.graph2d.DrawShapeConstants;
import xj.graph2d.GraphNodeBase;
import xj.graph2d.Label;
import xj.graph2d.TextShape;
import xj.graph2d.template.UMLTemplate;
import xj.graph2d.template.UMLUseCaseDiagramTemplate;
import xj.model.uml.UMLSystemBoundary;

public class UMLSystemBoundaryModelListener 
  extends AbstractModelListener {

  public UMLSystemBoundaryModelListener() {
  }

  public UMLSystemBoundaryModelListener(UMLTemplate template) {
    this(null, null, template);
  }

  public UMLSystemBoundaryModelListener(Object model, DrawShape shape,
					UMLTemplate template) {
    super(model, shape);
    this.template = template;
  }

  @Override
  public void updateModel() {
    if (shape instanceof GraphNodeBase && 
	model instanceof UMLSystemBoundary) {
      if (debug) { 
	System.out.println("UMLSystemBoundaryModelListener.updateModel()");
      }
      GraphNodeBase sysnode = (GraphNodeBase) shape;
      UMLSystemBoundary sysmodel = (UMLSystemBoundary) model;
      String name = sysnode.getLabel();
      if (name != null) {
	sysmodel.setName(name);
      }
    }
  }

  @Override
  public void updateShape() {
    if (shape instanceof GraphNodeBase && 
	model instanceof UMLSystemBoundary) {
      if (debug) { 
	System.out.println("UMLSystemBoundaryModelListener.updateShape()");
      }
      GraphNodeBase sysnode = (GraphNodeBase) shape;
      UMLSystemBoundary sysmodel = (UMLSystemBoundary) model;
      String name = sysmodel.getName();
      setLabel(sysnode, name);
    }
  }

  protected void setLabel(GraphNodeBase node, String label) {
    if (node != null) {
      Label curLabel = node.getLabelShape();
      if (curLabel != null) {
	if (label != null && label.length() > 0) {
	  curLabel.setText(label);
	  node.doLayout();
	} else {
	  node.removeLabel();
	}
      } else {
	if (label != null && label.length() > 0) {
	  String prompt = "System";
	  boolean multi = true;

	  TextShape labelShape;
	  if (template != null) {
	    labelShape = template.makeContentText(label, prompt, multi,
						  UMLUseCaseDiagramTemplate.BOUNDARY_FONT,
						  UMLUseCaseDiagramTemplate.BOUNDARY_COLOR);
	    labelShape.setMovable(false);
	  } else {
	    labelShape = new TextShape(label);
	    labelShape.setPrompt(prompt);
	    labelShape.setAllowMultiLine(multi);
	    labelShape.setDetachable(false);
	    labelShape.setSticky(true);
	    labelShape.setAllowRotation(false);
	    labelShape.setScalable(false);
	    labelShape.showCursor(true);
	  }
	  labelShape.setRole(DrawShapeConstants.ROLE_NODE_LABEL);
	  labelShape.setDisplayName("Name");

	  node.setLabel(labelShape);
	}
      }
    }
  }

  protected UMLTemplate template;

}
